<template>
  <div class="counter-warp" v-if="0">
    <div class="header">
      <div class="header-content">
        <div class="text">
          <div class="text-header">世纪金源物业</div>
        </div>
      </div>
    </div>
    <picker class="picker" v-if="0" :range="userIDList" :value="selectedIndex" @change="choose">
      <div class="cell">
        <div class="choose">用户</div>
        <div>
          {{userIDList[selectedIndex]}}
        </div>
      </div>
    </picker>
    <button hover-class="clicked" class="login-button" @click="handleLogin">登录</button>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { genTestUserSig } from '../../../static/utils/GenerateTestUserSig'
export default {
  data () {
    return {
      password: '',
      userIDList: new Array(30).fill().map((item, idx) => ('user' + idx)),
      selectedIndex: 1,
    }
  },
  computed: {
    ...mapState({
      myInfo: state => state.user.myInfo
    })
  },
  mounted () {
    wx.showLoading({ title: '', mask: true,duration:500 })
  },
  onShow(){
    // 做延时登录处理，防止触发重复登录，会触发踢出账号通知
    setTimeout(()=>{
      // this.handleLogin()
      wx.switchTab({ url: '/pages/index/main' })
    },200)
  },
  onUnload () {
  },
  methods: {
    // 点击登录进行初始化
    handleLogin() {
      var userMsg=wx.getStorageSync('userMsg');
      if(userMsg) {
        var userID = userMsg.im_userId
        // case1: 要登录的用户是当前已登录的用户，则直接跳转即可
        console.warn('this.myInfo',this.myInfo,this.myInfo.userID,userID)
        if (this.myInfo.userID && userID === this.myInfo.userID) {
          wx.switchTab({ url: '/pages/index/main' })
          return
        }
        // case3: 正常登录流程
        this.login(userMsg)
      }else {
        wx.switchTab({ url: '/pages/index/main' })
      }
    },
    login(userMsg) {
      console.warn('login=login')
      // var userID = 'user2'
      // let userSig = genTestUserSig(userID).userSig
      var userID = userMsg.im_userId
      var userSig = userMsg.userSig
      wx.$app.login({
        userID:userID,
        userSig: userSig
      }).then((res) => {
        console.warn('login=',res)
        this.$store.commit('setRtcConfig', {userID: userID, userSig: userSig, sdkAppID: wx.$sdkAppID})
        wx.switchTab({ url: '/pages/index/main' })
      }).catch(() => {
        wx.switchTab({ url: '/pages/index/main' })
      })
    },
    choose (event) {
      this.selectedIndex = Number(event.mp.detail.value)
      console.log('event',event)
    }
  }
}
</script>

<style lang="stylus" scoped>
.counter-warp
  background $white
  .header
    padding 30px 40px
    color #666
    .header-content
      display flex
      align-items center
      .icon
        width 76px
        height 50px
      .text
        text-align left
        padding-left 8px
        .text-header
          font-size 28px
        .text-content
          font-size 12px
  .picker
    width 40vw
    margin 80px auto 60px
    .cell
      display flex
      justify-content  space-between
      align-items center
      border-bottom  1px solid $border-base
      padding-bottom 12px
      .choose
        font-weight 600
        font-size 16px

.login-button
  width 40vw
  background-color $primary
  color white
  font-size 16px
  &::before
    width 20px
    height 20px
    margin 0 6px 2px 0
.clicked
  background-color $dark-primary
</style>
